import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Attr;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;


public class ConceptNameXMLParser {
static List conceptNameList = new ArrayList();
public static String domainName;
	
	public ConceptNameXMLParser()
	{	
	    
	    
	}
	
	
	public static void parseXML(File xmlFileToOpen) throws ParserConfigurationException, SAXException, IOException
	{
		 DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
	        DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
	        org.w3c.dom.Document doc = docBuilder.parse(xmlFileToOpen);
	        listChildren(doc);
	}
/*public static void parseXMl()
		{
		
			Builder builder = new Builder();
		     
		    try {
		      Document doc = builder.build("Concepts.xml");
		      Element root = doc.getRootElement();
		      listChildren(root, 0);      
		    }
		    // indicates a well-formedness error
		    catch (ParsingException ex) { 
		     // System.out.println(args[0] + " is not well-formed.");
		      System.out.println(ex.getMessage());
		    }  
		    catch (IOException ex) { 
		      System.out.println(ex);
		      
		    }  	
		}
@SuppressWarnings("unchecked")
public static void listChildren(Node current, int depth)

{
	 String data = "";
	    if (current instanceof Element) {
	        Element temp = (Element) current;
	        data = temp.getQualifiedName();   
	    }
	    else if (current instanceof ProcessingInstruction) {
	        ProcessingInstruction temp = (ProcessingInstruction) current;
	        data =  temp.getTarget();   
	    }
	    else if (current instanceof DocType) {
	        DocType temp = (DocType) current;
	        data =  temp.getRootElementName();   
	    }
	    else if (current instanceof Text || current instanceof Comment) {
	        String value = current.getValue();
	        value = value.replace('\n', ' ').trim();	
	        if (value.length() <= 20) data = ": " + value;
	        else data =  current.getValue().substring(0, 17);   
	        
	    }
	    // Attributes are never returned by getChild()
	   int childCount = 0; 
	   
	   
	   if(data.equalsIgnoreCase("Concept"))
	   {
		    childCount = current.getChildCount();
		    
		   // System.out.println(current.getChild(0).getValue());
		   // System.out.println(current.getChild(1).toString().substring(17, 25));
		    System.out.println(current.getValue()+ " " + current.getChild(0));
		    //conceptNameList.add(current.getChild(0).getValue());
		   
		    // tableData = new String [][] {{current.getChild(0).getValue()}}; 
		    
	   }
	   

		  // System.out.println( data);
		   for (int i = 0; i < current.getChildCount(); i++) {
			      listChildren(current.getChild(i), depth+1);
			    }
	  }
	*/


	private static void listChildren(org.w3c.dom.Document doc) {
		
		// TODO Auto-generated method stub
		NodeList nodeList = doc.getElementsByTagName("Concept");
		int length = nodeList.getLength();
		
		for (int i =0;i<length ; i++)
		{
		NamedNodeMap nodemap =	nodeList.item(i).getAttributes();
		conceptNameList.add(nodemap.item(0).getNodeValue());
		}
		
		NodeList node = doc.getElementsByTagName("Concepts");
		NamedNodeMap nodeMap = node.item(0).getAttributes();
		domainName = nodeMap.item(0).getNodeValue();
		
	}
	

}


